<template>
  <div class="bilibili-live-card">
    <div class="card-header">
      <h3 class="card-title">
        <el-icon><VideoCamera /></el-icon>
        正在直播
        <span class="live-count">({{ liveCount }}个)</span>
      </h3>
      <el-button type="text" size="small" @click="handleViewAll">
        查看更多
        <el-icon><ArrowRight /></el-icon>
      </el-button>
    </div>

    <div class="live-list">
      <div
          v-for="live in displayedLives"
          :key="live.id"
          class="live-item"
          @click="handleLiveClick(live)"
      >
        <div class="live-cover">
          <img :src="live.cover" :alt="live.title" />
          <div class="live-status">
            <span class="status-badge">直播中</span>
            <span class="viewer-count">
              <el-icon><View /></el-icon>
              {{ formatCount(live.viewerCount) }}
            </span>
          </div>
        </div>

        <div class="live-info">
          <h4 class="live-title" :title="live.title">{{ live.title }}</h4>
          <div class="anchor-info">
            <el-avatar :size="20" :src="live.anchorAvatar" />
            <span class="anchor-name">{{ live.anchorName }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 空状态 -->
    <div v-if="!lives.length" class="empty-live">
      <el-empty description="暂无直播" :image-size="60" />
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { VideoCamera, View, ArrowRight } from '@element-plus/icons-vue'

const props = defineProps({
  lives: {
    type: Array,
    default: () => []
  },
  title: {
    type: String,
    default: '正在直播'
  },
  maxDisplay: {
    type: Number,
    default: 3
  }
})

const emit = defineEmits(['live-click', 'view-all'])

const router = useRouter()

// 计算属性
const liveCount = computed(() => props.lives.length)

const displayedLives = computed(() => {
  return props.lives.slice(0, props.maxDisplay)
})

// 处理方法
const handleLiveClick = (live) => {
  emit('live-click', live)
  router.push(`/live/${live.id}`)
}

const handleViewAll = () => {
  emit('view-all')
  router.push('/live')
}

// 工具函数
const formatCount = (count) => {
  if (count >= 10000) {
    return (count / 10000).toFixed(1) + '万'
  }
  return count.toString()
}
</script>

<style scoped>
.bilibili-live-card {
  background: white;
  border-radius: 8px;
  padding: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid #f0f0f0;
}

.card-title {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #18191c;
  display: flex;
  align-items: center;
  gap: 6px;
}

.live-count {
  font-size: 12px;
  color: #999;
  font-weight: normal;
  margin-left: 4px;
}

.live-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.live-item {
  display: flex;
  gap: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  padding: 4px;
  border-radius: 6px;
}

.live-item:hover {
  background: #f5f7fa;
}

.live-cover {
  position: relative;
  width: 60px;
  height: 40px;
  border-radius: 4px;
  overflow: hidden;
  flex-shrink: 0;
}

.live-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.live-status {
  position: absolute;
  top: 2px;
  left: 2px;
  right: 2px;
  display: flex;
  justify-content: space-between;
}

.status-badge {
  background: #ff3860;
  color: white;
  padding: 1px 4px;
  border-radius: 2px;
  font-size: 10px;
}

.viewer-count {
  background: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 1px 4px;
  border-radius: 2px;
  font-size: 10px;
  display: flex;
  align-items: center;
  gap: 1px;
}

.live-info {
  flex: 1;
  min-width: 0;
}

.live-title {
  margin: 0 0 4px 0;
  font-size: 12px;
  font-weight: 500;
  color: #18191c;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  height: 2.6em;
}

.anchor-info {
  display: flex;
  align-items: center;
  gap: 4px;
}

.anchor-name {
  font-size: 10px;
  color: #999;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.empty-live {
  padding: 20px 0;
  text-align: center;
}
</style>